$(function(){
	var basePath=$("#basepath").val();
	$("#student_edit_form select[name='college']").change(function(){
		var collegeId=$(this).val();
		var $depSelect=$("#student_edit_form select[name='department']");
		$depSelect.html("");
		var $classSelect=$("#student_edit_form select[name='classInfo']");
		$classSelect.html("");
		$.get(basePath+ "admin/branch/getDepsOfCollege/"+collegeId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						if(data.error>0){
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							return;
						}
						for(i=0;i<data.length;i++){
							$(" <option/>").val(data[i].id).html(data[i].name).appendTo($depSelect);
						}
						if(data.length==0){
							$(" <option value='0'>不可用</option>").appendTo($depSelect);
							$(" <option value='0'>不可用</option>").appendTo($classSelect);
							makeAlert("无可用系,请添加之后再试","error",$("#student_edit_alert_container"),"no_dep_error");
						}else{
							$depSelect.change();
							$("#student_edit_alert_container .no_dep_error").alert('close');
						}
					}
			}, "json");
	});
	$("#student_edit_form select[name='department']").change(function(){
		var departmentId=$(this).val();
		var $classSelect=$("#student_edit_form select[name='classInfo']");
		$classSelect.html("");
		$.get(basePath+ "admin/class/getClassesByDep/"+departmentId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						if(data.error>0){
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							return;
						}
						for(i=0;i<data.length;i++){
							$(" <option/>").val(data[i].id).html(data[i].name).appendTo($classSelect);
						}
						if(data.length==0){
							$(" <option value='0'>无数据</option>").appendTo($classSelect);
							makeAlert("无可用班级,请添加之后再试","error",$("#student_edit_alert_container"),"no_class_error");
						}else{
							$("#student_edit_alert_container .no_class_error").alert('close');
						}
					}
			}, "json");
	});
	$("#student_edit_form_save").click(function(){
		var stuId=$("#student_edit_form input[name='student_id']").val();
		var stuNo=$("#student_edit_form input[name='student_no']").val();
		var stuName=$("#student_edit_form input[name='student_name']").val();
		var stuGender=$("#student_edit_form select[name='gender']").val();
		var stuGrade=$("#student_edit_form input[name='student_grade']").val();
		var stuQq=$("#student_edit_form input[name='student_qq']").val();
		var stuTel=$("#student_edit_form input[name='student_tel']").val();
		var stuClass=$("#student_edit_form select[name='classInfo']").val();
		if(stuClass==0){
			makeAlert("无可用班级,请添加之后再试","error",$("#student_edit_alert_container"),"no_class_error");
			return;
		}
		$("#student_edit_alert_container .empty_field_error").alert('close');
		$("#student_edit_alert_container .repeat_no_error").alert('close');
		$.post(basePath+ "admin/student/updateStudent",
				{id:stuId,no:stuNo,name:stuName,gender:stuGender,grade:stuGrade,qq:stuQq,telephone:stuTel,class_id:stuClass},
				function(data,textStatus){
					if(textStatus=="success"){
						switch (data) {
						case "-1":
							makeAlert("带*字段不允许为空,请重新检查","error",$("#student_edit_alert_container"),"empty_field_error");
							break;
						case "-2":
							alert("请勿进行非法操作");
							window.location=basePath;
							break;
						case "-3":
							makeAlert("已存在学号为"+stuNo+"的学生,请检查后再输入","error",$("#student_edit_alert_container"),"repeat_no_error");
							break;
						case "-4":
							alert("参数错误,请重试");
							history.go(0);
							break;
						case "0":
							noty({text:"更新成功,正在刷新"});
							window.location=document.referrer;
							break;
						default:
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							break;
						}
					}
			}, "text");
	});
	$("#student_edit_form_resetPass").click(function(){
		$("#student_reset_box_alert_container").html("");
		$("#student_reset_box input[name='newPass']").val("");
	});
	$("#student_reset_form_save").click(function(){
		var pass=$("#student_reset_box input[name='newPass']").val();
		var stuId=$("#student_edit_form input[name='student_id']").val();
		$("#student_reset_box_alert_container .empty_field_error").alert('close');
		$.post(basePath+ "admin/student/resetStudentPassword",
				{stuId:stuId,password:pass},
				function(data,textStatus){
					if(textStatus=="success"){
						switch (data) {
						case "-2":
							makeAlert("新密码不能为空,请重新检查","error",$("#student_reset_box_alert_container"),"empty_field_error");
							break;
						case "-1":
							alert("请勿进行非法操作");
							window.location=basePath;
							break;
						case "0":
							noty({text:"重置成功"});
							$("#student_reset_box").modal('hide');
							break;
						}
					}
				},"text");
	});
	//添加
	$("#student_add_form select[name='college']").change(function(){
		var collegeId=$(this).val();
		var $depSelect=$("#student_add_form select[name='department']");
		$depSelect.html("");
		var $classSelect=$("#student_add_form select[name='classInfo']");
		$classSelect.html("");
		$.get(basePath+ "admin/branch/getDepsOfCollege/"+collegeId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						if(data.error>0){
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							return;
						}
						for(i=0;i<data.length;i++){
							$(" <option/>").val(data[i].id).html(data[i].name).appendTo($depSelect);
						}
						if(data.length==0){
							$(" <option value='0'>不可用</option>").appendTo($depSelect);
							$(" <option value='0'>不可用</option>").appendTo($classSelect);
							makeAlert("无可用系,请添加之后再试","error",$("#student_add_alert_container"),"no_dep_error");
						}else{
							$depSelect.change();
							$("#student_add_alert_container .no_dep_error").alert('close');
						}
					}
			}, "json");
	});
	$("#student_add_form select[name='department']").change(function(){
		var departmentId=$(this).val();
		var $classSelect=$("#student_add_form select[name='classInfo']");
		$classSelect.html("");
		$.get(basePath+ "admin/class/getClassesByDep/"+departmentId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						if(data.error>0){
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							return;
						}
						for(i=0;i<data.length;i++){
							$(" <option/>").val(data[i].id).html(data[i].name).appendTo($classSelect);
						}
						if(data.length==0){
							$(" <option value='0'>无数据</option>").appendTo($classSelect);
							makeAlert("无可用班级,请添加之后再试","error",$("#student_add_alert_container"),"no_class_error");
						}else{
							$("#student_add_alert_container .no_class_error").alert('close');
						}
					}
			}, "json");
	});
	$("#student_add_form_save").click(function(){
		var stuNo=$("#student_add_form input[name='student_no']").val();
		var stuName=$("#student_add_form input[name='student_name']").val();
		var stuPass=$("#student_add_form input[name='student_pass']").val();
		var stuGender=$("#student_add_form select[name='gender']").val();
		var stuGrade=$("#student_add_form input[name='student_grade']").val();
		var stuQq=$("#student_add_form input[name='student_qq']").val();
		var stuTel=$("#student_add_form input[name='student_tel']").val();
		var stuClass=$("#student_add_form select[name='classInfo']").val();
		if(stuClass==0){
			makeAlert("无可用班级,请添加之后再试","error",$("#student_add_alert_container"),"no_class_error");
			return;
		}
		$("#student_add_alert_container .empty_field_error").alert('close');
		$("#student_add_alert_container .repeat_no_error").alert('close');
		$.post(basePath+ "admin/student/insertStudent",
				{no:stuNo,name:stuName,password:stuPass,gender:stuGender,grade:stuGrade,qq:stuQq,telephone:stuTel,class_id:stuClass},
				function(data,textStatus){
					if(textStatus=="success"){
						switch (data) {
						case "-1":
							makeAlert("带*字段不允许为空,请重新检查","error",$("#student_add_alert_container"),"empty_field_error");
							break;
						case "-2":
							alert("请勿进行非法操作");
							window.location=basePath;
							break;
						case "-3":
							makeAlert("已存在学号为"+stuNo+"的学生,请检查后再输入","error",$("#student_add_alert_container"),"repeat_no_error");
							break;
						case "-4":
							alert("参数错误,请重试");
							history.go(0);
							break;
						case "0":
							noty({text:"添加成功,正在刷新"});
							window.location=document.referrer;
							break;
						default:
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							break;
						}
					}
			}, "text");
	});
});